<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\App;
use think\exception\ValidateException;
use think\facade\Log;

/**
 * Qustiondetail控制器
 */
class Qustiondetail extends AdminBase
{

    protected $model = null;

    protected $validate = null;

    protected $relationSearch = false;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\Qustiondetail();

        $this->validate = \app\admin\validate\Qustiondetail::class;
    }

    public function index()
    {
        list($limit, $where, $sortArr) = $this->buildTableParames();
        $filters = $this->request->get('filter', '{}');
        $filters = json_decode($filters, true);
        $where2 = [];
        if (!empty($filters['id'])) {
            $where2[] = ['pid', '=', $filters['id']];
        }
        // halt($where2);
        // 查询所有的一级分类集合
        $question_type = table('qustiontype')->select()->toarray();
        $question_type = array_column($question_type, 'title', 'id');
        $list = $this->model
            ->where($where2)
            ->order('sort asc')
            ->paginate($limit);
        $lists = $list->items();
        // halt($this->model->getlastsql());
        foreach ($lists as $k => $v) {
            $lists[$k]['pid'] = isset($question_type[$v['pid']]) ? $question_type[$v['pid']] : '';
        }
        $data = [
            'code' => 1,
            'msg' => '',
            'count' => $list->total(),
            'data' => $lists,
        ];
        return json($data);
    }

    /**
     * 添加
     */
    public function add()
    {
        $post = $this->request->post();
        // $pname = table('qustiontype')->where(['id' => $post['pid']])->find();
        // $post['pname'] = !empty($pname) ? $pname['title'] : '';
        try {
            // $this->validate && validate($this->validate)->check($post);
            $result = $this->model->save($post);
            if ($result) {
                return success('添加成功！');
            }
            return error('添加失败');
        } catch (ValidateException $e) {

            return error($e->getError());
        } catch (\Exception $e) {
            return error('添加失败:' . $e->getMessage());
        }
    }



    /**
     * 下拉列表分页
     */
    public function selectPage()
    {

        try {
            list($limit, $where, $sortArr) = $this->buildTableParames();

            $page = $this->request->get('page', 1);
            $limit = $this->request->get('limit', 15);
            $show_id = $this->request->get('show_id', 'id'); //前端显示的value
            $query_field = $this->request->get('query_field', 'name'); //查询的参数名称
            $show_field = $this->request->get('show_field', 'name'); //前端显示的label
            $keyword = $this->request->get('keyword', ''); //查询的参数值

            $query_value = $this->request->get('query_value', ''); //编辑查询检索的值

            if ($query_value) {
                $where[] = [$show_id, '=', $query_value];
            }
            if ($keyword) {
                $where[] = [$query_field, 'LIKE', "%{$keyword}%"];
            }

            $fields = $show_id . ',' . $show_field;
            $count = $this->model
                ->where($where)
                ->count();
            $datalist = $this->model
                ->field($fields)
                ->where($where)
                ->page($page, $limit)
                ->order('sort asc')
                ->select();
            $list = [];
            foreach ($datalist as $index => $item) {
                $result = [
                    $show_id => isset($item[$show_id]) ? $item[$show_id] : '',
                    $show_field => isset($item[$show_field]) ? $item[$show_field] : '',
                ];
                $list[] = $result;
            }
            $data = [
                'code' => 1,
                'msg' => '',
                'count' => $count,
                'data' => $list,
            ];
            return json($data);
        } catch (\Exception $e) {
            Log::error("--------:" . $e);
            return error("获取数据失败");
        }
    }
}
